<?php
class MHDEV_View_Helper_GetUserAvatar extends Zend_View_Helper_Abstract
{
	/*
	 * Get the (gr)avatar from a user
	 * 
	 * @param int $uid
	 * @param boolean $src
	 * 
	 * return string image in html
	 */
	public function GetUserAvatar($uid,$src = FALSE)
	{
		// helper
		$baseUrl 	= new MHDEV_View_Helper_BaseUrl();
		$typ 		= array('gif','jpeg','jpg','png');
		
		// dummy
		$dummy 		= $baseUrl->baseUrl(). '/static/gfx/dummy.gif';
		
		// vars
		$avatar 	= FALSE;
		$check 		= FALSE;
		$gravatar	= FALSE;
		
		// db
		$user		= new User();
		
		// query
		$selUser	= $user->find($uid);
		
		if($selUser->count() > 0)
		{
			$resUser	= $selUser->toArray();
			
			if($resUser[0]['gravatar_active'] == 1)
			{
				$gravatar	= TRUE;
				$gmail		= md5(strtolower($resUser[0]['gravatar_mail']));
				$gsize		= $resUser[0]['gravatar_size'];
				$default	= 'monsterid';
				
				$source		= 'http://www.gravatar.com/avatar.php?gravatar_id=' . $gmail . '&default=' . $default. '&size=' . $gsize;
				
				$avatar		= '<img src="' . $source . '" title="Gravatar" />';
				$check		= TRUE;
			}
		}
		
		if(!$gravatar)
		{
			foreach($typ AS $filetyp)
			{
				$source = 'upload/avatar/' . intval($uid) . '.' . $filetyp;
				
	
				if(is_file($source))
				{
					$avatar = '<img src="' . $baseUrl->baseUrl(). '/' . $source . '" title="Avatar" />';
					$check 	= TRUE;
				}
			}
			
			if(!$avatar)
			{
				$avatar = '<img src="' . $dummy . '" title="Avatar" />';
			}
		}
		
		return $src ? $check : $avatar;
	}
}
?>